<?php

namespace Biz\model\GzDesignWeekActivity;

use Doraemon\pockets\datebase\BaseDbModel;
use Medoo\Medoo;

class GzDesignWeekActivityModel extends BaseDbModel
{

    public const _CONFIG_FILE_ = 'db_store';


    public const _CONFIG_SELECT_ = 'db_brand_activity';

    public const _DATABASE_ = 'brand_activity';


    public const _TABLE_NAME = 'gz_design_week_activity';

    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;

    // const BRAND_ID = 270;//qa
    const BRAND_ID = 3373;//pro
    const TASK_MAP = [
        ['task_id' => 1],
        ['task_id' => 2],
        ['task_id' => 3],
        ['task_id' => 4],
    ];

//    const NOTE_ID_MAP = ['0009vzb0000bonyd','0009vwc0000bonvw','0009vxa0000bonvw','0009viy0000bonzz'];//note id qa环境
    //todo 由于正式数据尚未提供 上线时需要清空  待正式数据提供之后 再做替换
//    const NOTE_ID_MAP = ['0065dny00007tpfb','0065do60000901xv','0060m1m00007vjn3','005yafy00007vjn3','0060uor00007vjn3','0060uou00007vjn3','0060uv500007vjn3'];//note id gray/pro
    const NOTE_ID_MAP = [];

    const END_TIME = 20230308; //活动截止时间 7号晚上23:59:59结束
   
    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return self::id();
    }

    public static function updateDataByWhere($data, $where): bool
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return true;
    }

    public static function getMapByUid($uid): array
    {
        $where =[
            'uid' => $uid,
            'status' => GzDesignWeekActivityModel::STATUS_NORMAL,
        ];
        $data = self::select('*', $where) ?? [];
        return array_column($data, null, 'task_id');

    }


    public static function countByWhere($where)
    {
        return (int)self::count($where);
    }

    public static function countCompleteTaskByUid($uid)
    {
        $where['uid']=$uid;
        $where['status']=self::STATUS_NORMAL;
        return (int)self::count($where);
    }

    public static function selectByWhere($where, $field = '*'): array
    {
        return self::select($field, $where) ?? [];
    }
    public static function getCountMaps($map)
    {
        return self::select(['cnt' => Medoo::raw('count(uid)'), 'task_id'], $map);
    }

}